/*
 *   JVending
 *   Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


package org.jvending.provisioning.config;

import java.io.InputStream;
import java.io.IOException;

import java.util.*;
import java.util.logging.*;

import java.lang.reflect.Constructor;

import javax.provisioning.matcher.AttributeMatcher;

import org.jvending.registry.Repository;
import org.jvending.registry.RepositoryRegistry;
import org.jvending.registry.jaxb.JaxbConfiguration;

import org.jvending.provisioning.config.matchers.*;

/**
 * @author Shane Isbell
 * @since 1.3a
 */

public final class AttributeMatcherRepository implements Repository {

    private static Map matcherMap = new HashMap();

    private static Logger logger = Logger.getLogger("AttributeMatcherRepository");

    public AttributeMatcher getMatcherFor(String attributeName) {
        return (AttributeMatcher) matcherMap.get(attributeName);
    }

    public void load(InputStream inputStream, Hashtable properties) throws IOException {
        if (properties == null)
            throw new IOException("JV-1301-001: Null properties values for the attribute matcher repository");
        if (inputStream == null)
            throw new IOException("JV-1301-002: Null inputstream for the attribute matcher repository");

        Matchers matchers = (Matchers) JaxbConfiguration.parse(inputStream, (String) properties.get("binding-package"));

        for (Iterator i = matchers.getMatcher().iterator(); i.hasNext();) {
            MatcherType matcher = (MatcherType) i.next();
            String attributeName = matcher.getAttributeName();
            List initParam = matcher.getInitParam();
            AttributeMatcher attributeMatcher = null;
            try {
                String matcherClassName = matcher.getMatcherClass();
                Class matcherClass = Class.forName(matcherClassName);
                Constructor matcherConstructor = matcherClass.getConstructor(null);
                attributeMatcher = (AttributeMatcher) matcherConstructor.newInstance(null);
                attributeMatcher.init(attributeName, toMap(initParam));
            } catch (Exception e) {
                logger.log(Level.SEVERE, "JV-1301-003: Problem loading the attribute matcher repository", e);
                throw new IOException("JV-1301-004: Problem loading the attribute matcher repository");
            }
            matcherMap.put(attributeName, attributeMatcher);
        }
    }

    private Map toMap(List initParam) {
        Map map = new HashMap();
        for (Iterator i = initParam.iterator(); i.hasNext();) {
            InitParamType initParamType = (InitParamType) i.next();
            String name = initParamType.getParamName();
            String value = initParamType.getParamValue();
            map.put(name.trim(), value.trim());
        }
        return map;
    }

    public void setRepositoryRegistry(RepositoryRegistry repositoryRegistry) {
        //don't need this
    }

}